<?php

namespace app\adminapi\controller;

use think\Controller;
use think\facade\Request;
use app\adminapi\model\Role as roletable;

class Role extends BaseApi
{
    public function index(){
        // $this->ok("sbhd");
        //接受参数
        $param=Request::param();
        //查询数据
        $pageSize=$param['pageSize'];
        if(!empty($param['keyword'])){
            $keyword=$param['keyword'];
            // $data=roletable::where('role_name','like',"%$keyword%")->paginate($pageSize);
            $data=roletable::alias('t1')->join('pyg_auth t2','t1.`role_auth_ids`=t2.`id`','left')
            ->field('t1.*,t2.auth_name')->where('role_name','like',"%$keyword%")->paginate($pageSize);
        }else{
            // $data=roletable::paginate($pageSize);
            $data=roletable::alias('t1')->join('pyg_auth t2','t1.role_auth_ids=t2.id','left')
            ->field('t1.*,t2.auth_name')->paginate($pageSize);
        }
        $this->ok($data);

    }

//  修改数据
   public function edit(){
    // 测试
    // $this->ok('ababa');
    // 获取数据
    $param=Request::param();
    // dump($param);
    // 验证规则
    $rule=[
        'role_name|角色名'=>'max:50',
        'role_auth_ids|描述'=>'max:50',
    ];
    // 错误信息
    $msg=[
        'role_name'=>'角色名不能超过50',
        'role_auth_ids'=>'描述不能超过50',
    ];
    $validate=new \think\Validate($rule,$msg);

    if(!$validate->check($param)){
        $err=$validate->getError();
        $this->fail($err);
    }
    // 修改数据
    // admintable::update($param,['id'=>$param['id']]);
    roletable::where('id',$param['id'])->update(['role_name'=>$param['role_name'],'role_auth_ids'=>$param['role_auth_ids']]);
    $info=roletable::find($param['id']);
    // 返回数据
    $this->ok($info);
}

// //  删除数据
//     public function delete(){
//         // 获取数据

//         $param=Request::param();

// //      执行删除
//         admintable::where('id',$param['id'])->delete();

//         $this->ok();

//     }
}
